namespace Triad.Web.Admin.ASPMembership
{
    using System;
    using System.Configuration;
    using Triad.Web;

    public partial class EditProfile2 : Triad.Web.UI.Page
    {
        private bool firstNameChanged = false;
        private bool lastNameChanged = false;
        private bool emailChanged = false;
        private bool phoneChanged = false;
        private bool addressChanged = false;
        private bool cityChanged = false;
        private bool stateChanged = false;
        private bool zipCodeChanged = false;
        private bool themeChanged = false;
        private bool preference1Changed = false;
        private bool preference2Changed = false;
        private bool preference3Changed = false;
        


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                PopulateCategories();

                WebProfile pc = WebProfile.GetProfile(User.Identity.Name);
                txtFirstName.Text = pc.FirstName;
                txtLastName.Text = pc.LastName;
                txtEmail.Text = pc.Email;
                txtPhone.Text = pc.Phone;
                txtAddress.Text = pc.Address;
                txtCity.Text = pc.City;
                txtState.Text = pc.State;
                txtZipcode.Text = pc.Zipcode;
                ddlTheme.SelectedValue = pc.Theme.ToString();
                //ddlPreference1.SelectedValue = pc.Preference1.ToString();
                //ddlPreference2.SelectedValue = pc.Preference2.ToString();
                //ddlPreference3.SelectedValue = pc.Preference3.ToString();

            }
        }



        private void PopulateCategories()
        {
            //DataTable catDt=null;
            //if (Cache["categoriesDt"] == null)
            //{
            // DataSet ds= 
            //     SqlHelper.ExecuteDataset(GetConnectionString("localSqlServer"), "dbo.GetCategories", null);
            //    catDt=ds.Tables[0];
            //    if(catDt.Rows.Count>0)
            // Cache["categoriesDt"] = catDt;
            //}
            //else
            //{
            //    catDt = (DataTable)Cache["categoriesDt"];
            //}
            //    this.ddlPreference1.DataSource = catDt;
            //    ddlPreference1.DataTextField = "CategoryName";
            //    ddlPreference1.DataValueField = "ID";
            //    ddlPreference1.DataBind();
            //    this.ddlPreference2.DataSource = catDt;
            //    ddlPreference2.DataTextField = "CategoryName";
            //    ddlPreference2.DataValueField = "ID";
            //    ddlPreference2.DataBind();
            //    this.ddlPreference3.DataSource = catDt;
            //    ddlPreference3.DataTextField = "CategoryName";
            //    ddlPreference3.DataValueField = "ID";
            //    ddlPreference3.DataBind(); 


        }



        internal static string GetConnectionString(string specifiedConnectionString)
        {
            if (String.IsNullOrEmpty(specifiedConnectionString))
                return null;

            // Check <connectionStrings> config section for this connection string
            ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings[specifiedConnectionString];
            if (connObj != null)
                return connObj.ConnectionString;

            return null;
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            WebProfile pc = WebProfile.GetProfile(User.Identity.Name);

            if (firstNameChanged) {pc.FirstName = txtFirstName.Text;}
            if (lastNameChanged){pc.LastName = txtLastName.Text;}
            if (emailChanged){pc.Email = txtEmail.Text;}
            if (phoneChanged){pc.Phone = txtPhone.Text;}
            if (addressChanged){pc.Address = txtAddress.Text;}
            if (cityChanged){pc.City = txtCity.Text;}
            if (zipCodeChanged){pc.Zipcode = txtZipcode.Text;}
            if (stateChanged){pc.State = txtState.Text;}
            if (themeChanged) { pc.Theme = ddlTheme.Text; }

            if (preference1Changed) pc.Preference1 = 1; 
            if (preference2Changed) pc.Preference2 = 2;
            if (preference3Changed) pc.Preference3 = 3;

            pc.Save();

        }
        protected void txtFirstName_TextChanged(object sender, EventArgs e)
        {
            firstNameChanged = true;
        }

        protected void txtLastName_TextChanged(object sender, EventArgs e)
        {
            lastNameChanged = true;
        }

        protected void txtEmail_TextChanged(object sender, EventArgs e)
        {
            emailChanged = true;
        }

        protected void txtPhone_TextChanged(object sender, EventArgs e)
        {
            phoneChanged = true;
        }

        protected void txtAddress_TextChanged(object sender, EventArgs e)
        {
            addressChanged = true;
        }

        protected void txtCity_TextChanged(object sender, EventArgs e)
        {
            cityChanged = true;
        }

        protected void txtState_TextChanged(object sender, EventArgs e)
        {
            stateChanged = true;
        }

        protected void txtZipcode_TextChanged(object sender, EventArgs e)
        {
            this.zipCodeChanged = true;
        }

        protected void ddlPreference1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.preference1Changed = true;

        }
        protected void ddlPreference2_SelectedIndexChanged(object sender, EventArgs e)
        {

            this.preference2Changed = true;
        }
        protected void ddlPreference3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.preference3Changed = true;

        }
        protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.themeChanged = true;
        }
    }
}